﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Management;
using System.Configuration;


namespace HealthMonitoring
{

    public class MemoryDumpEvent : WebErrorEvent
    {
        private static MemoryDumpDiagnosticsSection _memDumpConfig = null;
        private static bool _isValidMemoryDumpSectionPresent = false;
        internal static Exception _memoryDumpSectionException = null;
        private static int _eventCodeOffSet = IsMemoryDumpSectionPresent();
        private static int _miniDumpEventCode = WebEventCodes.WebExtendedBase + _eventCodeOffSet;
      

        internal static MemoryDumpDiagnosticsSection MemoryDumpConfig
        {
            get
            {
                return _memDumpConfig;
            }
        }


        /// <summary>
        /// Use this constructor when Custom Event Code is to be used.
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="eventSource"></param>
        /// <param name="eventCode"></param>
        /// <param name="ex"></param>
        public MemoryDumpEvent(string msg,
            object eventSource,
            int eventCode, Exception ex)
            :
            base(msg, eventSource, eventCode, ex)
        {


        }


        /// <summary>
        /// Use this constructor when Default Event Code is to be used for logging.
        /// This constructor also checks if the minidump is already created for current exception instance or not
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="eventSource"></param>
        /// <param name="ex"></param>
        public MemoryDumpEvent(string msg,
            object eventSource,
              Exception ex)
            :
                this(msg, eventSource, _miniDumpEventCode, ex)
        {
            if (!_isValidMemoryDumpSectionPresent)
            {
                MemoryDumpManager.RaiseDumpCreationFailedException(MemoryDumpManager.Constants.ErrorMessageForInvalidMDDSection, _memoryDumpSectionException, ex);
            }
        }





        /// <summary>
        /// Raises the MiniDumpEvent.
        /// </summary>
        public override void Raise()
        {
            if (_isValidMemoryDumpSectionPresent)
            {
                base.Raise();
            }
        }

        /// <summary>
        /// This method looks for Memory Dump Diagnostics section in web config
        /// If MemoryDumpDiagnostic section is found, it is loaded in static variable and used through out the session
        /// It returns Off set value for event code.
        /// </summary>
        /// <returns></returns>
        private static int IsMemoryDumpSectionPresent()
        {
            try
            {
                _memDumpConfig = ConfigurationManager.GetSection(MemoryDumpManager.Constants.MemoryDumpDiagnosticsSectionName) as MemoryDumpDiagnosticsSection;
            }
            catch (Exception Ex)
            {
                _memoryDumpSectionException = Ex;
                return MemoryDumpManager.Constants.DefaultOffSetCode;
            }
            if (_memDumpConfig == null || !_memDumpConfig.Enabled)
            {
                return MemoryDumpManager.Constants.DefaultOffSetCode;
            }
            _isValidMemoryDumpSectionPresent = true;
            return _memDumpConfig.EventCodeOffSet;

        }



    }
}
